import { editStatusApi } from "@/api/configCenter/accountKeeper";
import { message } from "ant-design-vue";
import { Ref } from "vue";

export default function OperationHook(state: {
  formState: AccountKeeper.editForm;
  loading: Ref<boolean>;
  emit: {
    (e: "submit"): void;
    (e: "update:visible", visible: boolean): void;
  };
  formDataRef: Ref<any>;
  passData: Ref<AccountKeeper.ListRows>;
}) {
  const { formState, loading, emit, formDataRef, passData } = state;

  const onSubmit = () => {
    formDataRef.value.validate().then(() => {
      loading.value = true;
      editStatusApi({ ...formState, id: passData.value.id })
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };

  const resetForm = () => {
    formDataRef.value.resetFields();
    emit("update:visible", false);
  };

  return {
    onSubmit,
    resetForm
  };
}
